<?php
require_once (__DIR__ . "/../inc/functions.php");
require_once (__DIR__ . "/Keywords.php");

class paymentMethods
{
	static function GetAll()
	{
		$conn = GetConnection();
		$results = $conn->query('SELECT * FROM paymentMethods');
		$error=$conn->error;
		return $results;
	}
	
	static function Get($id)
	{
		$conn = GetConnection();
		$results = $conn->query("SELECT * FROM paymentMethods WHERE id=$id");
		$row = $results->fetch_assoc();
		$error=$conn->error;		
		return $row;
	}
	
	static function GetForUser($userid)
	{
		$conn = GetConnection();
		$results = $conn->query("SELECT * FROM paymentMethods WHERE userId=$userid");
		$error=$conn->error;	
//		print_r($error);	
		return $error != '' ? false : $results;
	}
	

	static function Insert($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = 	"INSERT INTO paymentMethods (paymentType,accountType,accountNumber,userId,expDate) "
			.	"Values ($row2[paymentType],$row2[accountType],$row2[accountNumber],$row2[userId],'$row2[expDate]' ); ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Update($row)
	{
		//print_r ($row);	
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = 	"UPDATE paymentMethods "
			.	"Set paymentType=$row2[paymentType],accountType=$row2[accountType],accountNumber=$row2[accountNumber],userId=$row2[userId],expDate='$row2[expDate]' "
			.	"WHERE id=$row2[id] ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Delete($id)
	{
		$conn = GetConnection();
		$sql = 	"DELETE FROM paymentMethods "
			.	"WHERE id=$id ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;				
	}
	static function Validate($row)
	{
		$results=array();
		if(empty($row['accountNumber'])) $results['accountNumber'] = 'Account Number is required';		
		if(empty($row['expDate'])) $results['expDate'] = 'Expiration date is required';		
		return count($results) > 0 ? $results : true;
	}
}

?>
